using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class TaiKhoanKeToanDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveTaiKhoanKeToan(TaiKhoanKeToan taikhoanketoan)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddTaiKhoanKeToan", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddTaiKhoanKeToan";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, taikhoanketoan.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TenTK", DbType.String, ParameterDirection.Input, taikhoanketoan.TenTK);
			cmd.Parameters.Add(param);
			param = CreateParameter("@MaSoTK", DbType.String, ParameterDirection.Input, taikhoanketoan.MaSoTK);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TKCha", DbType.String, ParameterDirection.Input, taikhoanketoan.TKCha);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoDuBanDau", DbType.Int64, ParameterDirection.Input, taikhoanketoan.SoDuBanDau);
			cmd.Parameters.Add(param);
            param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
            cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();

            result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			return result;
		}
		
		// Get all TaiKhoanKeToan in DB
		public ResultObj GetAllTaiKhoanKeToan( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<TaiKhoanKeToan> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllTaiKhoanKeToan", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<TaiKhoanKeToan>();
			TaiKhoanKeToan item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new TaiKhoanKeToan();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.TenTK = Convert.ToString(reader["TenTK"]);
			item.MaSoTK = Convert.ToString(reader["MaSoTK"]);
			item.TKCha = Convert.ToString(reader["TKCha"]);
			item.SoDuBanDau = Convert.ToInt64(reader["SoDuBanDau"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all TaiKhoanKeToan in DB with condition
		public ResultObj GetAllTaiKhoanKeToanByID(TaiKhoanKeToan taikhoanketoan)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<TaiKhoanKeToan> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllTaiKhoanKeToanByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, taikhoanketoan.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<TaiKhoanKeToan>();
			TaiKhoanKeToan item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new TaiKhoanKeToan();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.TenTK = Convert.ToString(reader["TenTK"]);
			item.MaSoTK = Convert.ToString(reader["MaSoTK"]);
			item.TKCha = Convert.ToString(reader["TKCha"]);
			item.SoDuBanDau = Convert.ToInt64(reader["SoDuBanDau"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a TaiKhoanKeToan out of DB with condition
		public ResultObj DeleteTaiKhoanKeToan(TaiKhoanKeToan taikhoanketoan)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteTaiKhoanKeToan", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteTaiKhoanKeToan";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, taikhoanketoan.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
					
	}
}
